﻿using System;
using System.Globalization;
using System.Windows.Data;

namespace MaxStoreShop.Core.Converters
{
    public class MoneyEditorConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value == null)
                return 0;

            int kopec;
            if (!int.TryParse(value.ToString(), out kopec))
                return null;

            return kopec / 100.00;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            var val = value as double?;
            if (!val.HasValue)
                return 0;

            var y = System.Convert.ToInt32(val.Value*100);
            return y;
        }
    }
}
